﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GetRawWeb
{
    public class SiteWeb : FolderWeb
    {
        public SiteWeb(Uri uri) : base(uri)
        {
            this.Site = uri;
            ListLinkCheck = new Dictionary<LinkWeb, bool>();
            Raw();
        }

        public Dictionary<LinkWeb,bool> ListLinkCheck { get; set; }
        public Uri Site { get; set; }

        public void Raw()
        {
            var folderWeb = new FileWeb(Site);
            var linkWebs = folderWeb.GetLinkWebs();
            foreach (var linkWeb in linkWebs)
            {
                if (!ListLinkCheck.ContainsKey(linkWeb))
                    ListLinkCheck.Add(linkWeb,false);
            }
            for (;;)
            {
                var link = ListLinkCheck.FirstOrDefault(x => !x.Value&&x.Key.Type==TypePage.A);
                var c = ListLinkCheck.Count(x => !x.Value && x.Key.Type == TypePage.A);
                if (link.Key == null)
                    break;
                ListLinkCheck[link.Key] = true;
                var linkWeb = new FileWeb(link.Key.Uri);
                foreach (var linkw in linkWeb.GetLinkWebs())
                {
                    if (linkw==null) continue;
                    if (ListLinkCheck.All(x => x.Key.Uri != linkw.Uri))
                    {
                        ListLinkCheck.Add(linkw, false);
                        Console.WriteLine(linkw.Uri.PathAndQuery);
                    }
                }
                Console.Title = string.Format("{0}/{1}", c, ListLinkCheck.Count);
            }
            
//            foreach (var link in ListLinkCheck.Where(x=>x.Type== TypePage.A))
//            {
//                var linkWeb = new FileWeb(link.Uri);
//                foreach (var linkw in linkWeb.GetLinkWebs())
//                {
//                    if (linkw==null) continue;
//                    ListLinkCheck.Add(linkw);
//                }
//            }
        }

    }
}
